package org.base.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.base.ConstantPool;
import org.base.common.annotation.Log;
import org.base.common.annotation.RequiredPerm;
import org.base.common.cache.CacheTemplate;
import org.base.common.controller.BaseController;
import org.base.common.exception.BusinessException;
import org.base.common.exception.ExceptionEnum;
import org.base.common.pojo.PageResult;
import org.base.common.token.JWTToken;
import org.base.common.util.TokenUtils;
import org.base.common.util.UserUtils;
import org.base.sys.VO.*;
import org.base.sys.constant.SysConstant;
import org.base.sys.enums.BizType;
import org.base.sys.enums.Sex;
import org.base.sys.enums.UserStatus;
import org.base.sys.pojo.SysUser;
import org.base.sys.query.SysUserQuery;
import org.base.sys.service.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.List;

//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.subject.Subject;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2020-05-27
 */
@Api(value = "用户信息", tags = "用户信息")
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/sys-user")
@Slf4j
public class SysUserController extends BaseController<SysUser, SysUserService> {


    private CacheTemplate cacheTemplate;

    /**
     * 添加
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加用户信息")
    @Log(bizType = BizType.INSERT)
    public boolean add(@Valid @RequestBody SysUser entity) {
        return service.save(entity);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询用户信息")
    public SysUserVO get(@PathVariable("id") Long id) {
        return service.getUserVOById(id);
    }

    /**
     * 更新
     */
    @PutMapping("/update")
    @ApiOperation(value = "根据id更新用户信息")
    @Log(bizType = BizType.UPDATE)
    public boolean update(@Valid @RequestBody SysUser entity) {
        return service.updateById(entity);
    }

    /**
     * 查询
     */
    @GetMapping("/query")
    @ApiOperation(value = "列表查询用户信息")
    public List<SysUser> query(SysUserQuery query) {
        return service.getByQuery(query);
    }

    /**
     * 分页查询
     */
    @GetMapping("/pageQuery")
    @ApiOperation(value = "分页查询用户信息")
    public PageResult pageQuery(SysUserQuery query) {
        return service.getByPageQuery(query);
    }


    /**
     * 添加或者保存
     */
    @PostMapping("/saveOrUpdate")
    @Log(bizType = BizType.INSERT_UPDATE)
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public boolean saveOrUpdate(@Valid @RequestBody SysUserVO entity) throws NoSuchAlgorithmException {
        return service.saveOrUpdateUser(entity);
    }


    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @RequiredPerm(value = SysConstant.ANON, editable = false)
    public String login(@RequestBody @Valid LoginVO loginVO) throws NoSuchAlgorithmException {
        final String key = loginVO.getKey();
        final String code = (String) cacheTemplate.get(key);
        if (StringUtils.isBlank(code) || !code.equalsIgnoreCase(loginVO.getCode())) {
            BusinessException.throwEx(ExceptionEnum.ERROR_VERIFICATION_CODE);
        }
        cacheTemplate.delete(key);
        service.login(loginVO.getUserName(), loginVO.getPassword());
        final SysUserVO userVO = service.getUserVOByLoginName(loginVO.getUserName());
        final JWTToken jwtToken = new JWTToken();
        jwtToken.setUserId(userVO.getId());
        jwtToken.setUserName(userVO.getUserName());
        jwtToken.setExpMin(ConstantPool.TOKEN_EXP);
        return TokenUtils.getToken(jwtToken);
    }

    @GetMapping("/unAuth")
    @ApiOperation(value = "未登录")
    public String unAuth(@RequestBody @Valid LoginVO loginVO) {
        BusinessException.throwEx(ExceptionEnum.UN_AUTH);
        return null;
    }


    @GetMapping("/info")
    @ApiOperation(value = "查询信息")
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public SysUserVO info() {
        return service.getUserVO(UserUtils.getUserId());
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public boolean logout() {
        return true;
    }


    @GetMapping("/userMenus")
    @ApiOperation(value = "查询用户菜单")
    public List<MenuVO> getUserMenus() {
        return service.getUserMenus(UserUtils.getUserId());
    }

    @GetMapping("/userStatus")
    public UserStatus[] getUserStatus() {
        return UserStatus.values();
    }

    @GetMapping("/sex")
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public Sex[] getSex() {
        return new Sex[]{Sex.MALE, Sex.FEMALE};
    }

    @GetMapping("/userProfile")
    @ApiOperation(value = "查询用户简介")
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public SysUserProfileVO getUserProfile() {
        return service.getUserProfile();
    }

    @PostMapping("/updateUserProfile")
    @Log(bizType = BizType.UPDATE)
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public boolean updateUserProfile(@Valid @RequestBody SysUserVO entity) {
        return service.updateUserProfile(entity);
    }

    @PostMapping("/changePassword")
    @Log(bizType = BizType.UPDATE)
    @RequiredPerm(value = SysConstant.LOGIN, editable = false)
    public boolean changePassword(@Valid @RequestBody ChangePasswordVO changePasswordVO) throws NoSuchAlgorithmException {
        return service.changePassword(changePasswordVO);
    }

    @PostMapping("/resetPassword")
    @Log(bizType = BizType.UPDATE)
    public boolean resetPassword(@Valid @RequestBody ResetPasswordVO resetPasswordVO) throws NoSuchAlgorithmException {
        return service.resetPassword(resetPasswordVO);
    }

}

